package com.tongji.housediy.view.components
{
	import caurina.transitions.Tweener;
	
	import com.tongji.housediy.events.commonEvent.RotationRelatedEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.utils.CollisionDetection;
	import com.tongji.housediy.utils.LoadAdmin;
	import com.tongji.housediy.utils.MathCalculation;
	import com.tongji.housediy.vo.Goods;
	import com.tongji.housediy.vo.Model;
	import com.tongji.housediy.vo.PicModel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.events.CloseEvent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class Furniture extends RoomShape
	{
		public var content:BitmapData;
		
		public var mapPicSrc:String;
		public var dragPicSrc:String;
		public var modelSrc:String;
		public var modelZPosition:int;
		
		private var image:Image;
		private var parentRoomBoundingBox:Rectangle;
		
		// 临时变量，用于碰撞后还原
		private var tempPicModel:PicModel;
		private var tempWidth:Number;
		private var tempHeight:Number;
		
		private var tempRotation:Number;
		private var tempRectangle:Rectangle;
		private var tempValue:Number;
		
		[Embed(source="assets/images/warning.png")]
		private var BulletCritical:Class;
		private var alert:Alert;
		
		public function Furniture(hostGoods:Goods, content:BitmapData)
		{
			super();
			
			this.content = content;
			
			image = new Image();
			image.source = new Bitmap(content);
			//消除旋转和缩放后的锯齿，平滑处理
			image.smoothBitmapContent = true;
			
			image.width = content.width;
			image.height = content.height;
			
			image.x = -image.width / 2;
			image.y = -image.height / 2;
			
			this.addChild(image);			
			
			this.hostGoods = hostGoods;
			this.selectedColorID = hostGoods.selectedColorID;
			this.selectedSizeID = hostGoods.selectedSizeID;
			this.mapPicSrc = hostGoods.mapPicSrc;
			this.dragPicSrc = hostGoods.dragPicSrc;
			this.modelSrc = hostGoods.models[0].modelSrc;
			this.modelZPosition = hostGoods.models[0].zPosition;
		}
		
		override protected function mouseDownHandler(event:MouseEvent):void
		{
			// 隐藏 InfoWindow
			this.dispatchEvent(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
			if (this.parent.parent.name != "root1")
			{
				parentRoomBoundingBox = (this.parent.parent as AbstractDrawingRoom).updateBoundingAndCenterPoint();
			}
			
			super.mouseDownHandler(event);
		}
		
		override protected function mouseUpHandler(event:MouseEvent):void
		{
			// 显示 TitleWindow
			dispatchShowInfoEvent();
			
			super.mouseUpHandler(event);
			
			var rectangle:Rectangle = updateBoundingAndCenterPoint();
			
			var initialX:Number = rectangle.width / 2 - 11.5 + rectangle.x;
			var initialY:Number = rectangle.height + 20 + rectangle.y;
			
			this.dispatchEvent(new RotationRelatedEvent(RotationRelatedEvent.SHOW_ROTATION_BTN, initialX, initialY, this));
		}
		
		override protected function enterFrameHandler(event:Event):void
		{			
			this.dispatchEvent(new RotationRelatedEvent(RotationRelatedEvent.HIDE_ROTATION_BTN));
			
			//自定义旋转
			super.enterFrameHandler(event);
			
			this.x += this.offsetX;
			this.y += this.offsetY;
			if (this.parent.parent.name != "root1")
			{
				if (CollisionDetection.isCollisionWithOther(this)
					|| ((this.parent.parent as AbstractDrawingRoom).updateBoundingAndCenterPoint().width != parentRoomBoundingBox.width)
					|| ((this.parent.parent as AbstractDrawingRoom).updateBoundingAndCenterPoint().height != parentRoomBoundingBox.height))
				{
					this.x -= this.offsetX;
					this.y -= this.offsetY;
				}
			}		
		}
		
		override public function moveShape(xOffset:Number = 0, yOffset:Number = 0):Boolean
		{
			this.x += xOffset;
			this.y += yOffset;
			
			if (CollisionDetection.isCollisionWithOther(this))
			{
				this.x -= xOffset;
				this.y -= yOffset;
				return false;
			}
			else
				return true;
		}
		
		private function dispatchShowInfoEvent():void
		{
			var rectangle:Rectangle = updateBoundingAndCenterPoint();
			var secondPoint:Point = new Point(rectangle.x + rectangle.width / 2 + rectangle.width, rectangle.y + rectangle.height / 2 - rectangle.height);
			var windowPoint:Point = this.parent.parent.parent.localToGlobal(secondPoint);
			
			this.dispatchEvent(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.SHOW_TITLE_WINDOW,
				rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2,
				secondPoint.x, secondPoint.y,
				windowPoint.x, windowPoint.y,
				this));
		}
		
		public function rotationFurniture(value:Number):void
		{
			var alert:Alert;
			tempRotation = this.rotation;
			tempValue = value;
			tempRectangle = this.getRect(this.parent);
			MathCalculation.transformWithExternalPoint(this, new Point(tempRectangle.x + tempRectangle.width / 2, tempRectangle.y + tempRectangle.height / 2), ((value - tempRotation) % 360) * Math.PI / 180);
			
			if (CollisionDetection.isCollisionWithOther(this))
			{
				alert = Alert.show("旋转后发生碰撞，请重试！", "警告", Alert.OK, this.stage as Sprite, retrieveRotationHandler);
				alert.titleIcon = BulletCritical;
			}
		}
		
		private function retrieveRotationHandler(event:CloseEvent):void
		{
			MathCalculation.transformWithExternalPoint(this, new Point(tempRectangle.x + tempRectangle.width / 2, tempRectangle.y + tempRectangle.height / 2), ((tempRotation - tempValue) % 360) * Math.PI / 180);
		}
		
		override public function changeSizeAndColor(picModel:PicModel, bitmapData:BitmapData = null, hostGoods:Goods = null):void
		{
			// 保存临时变量
			this.tempWidth = image.width;
			this.tempHeight = image.height;
			
			this.tempPicModel = picModel;
			
			var loader:Loader = LoadAdmin.loadBitmapData(picModel.dragPicSrc);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
		}
		
		private function loadCompleteHandler(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE, loadCompleteHandler);
			
			image.source = event.target.content;
			image.width = (event.target.content as Bitmap).width;
			image.height = (event.target.content as Bitmap).height;
			
			Tweener.addTween(this, {time: 0.1, onComplete: testCollisionHandler});
		}
		
		private function testCollisionHandler():void
		{
			if (CollisionDetection.isCollisionWithOther(this))
			{
				alert = Alert.show("改变尺寸后发生碰撞，请重试！", "警告", Alert.OK, this.stage as Sprite, retrieveSizeHandler);
				alert.titleIcon = BulletCritical;
			}
			else
			{
				this.mapPicSrc = tempPicModel.mapPicSrc;
				this.dragPicSrc = tempPicModel.dragPicSrc;
				this.modelSrc = tempPicModel.model.modelSrc;
				this.modelZPosition = tempPicModel.model.zPosition;
				this.selectedSizeID = tempPicModel.selectedSizeID;
				this.selectedColorID = tempPicModel.selectedColorID;
				this.content = (image.source as Bitmap).bitmapData;
			}
		}
		
		private function retrieveSizeHandler(event:CloseEvent):void
		{
			image.source = this.dragPicSrc;
			image.width = tempWidth;
			image.height = tempHeight;
		}
		
		public function clone():Furniture
		{
			var newFurniture:Furniture = new Furniture(this.hostGoods, this.content.clone());
			newFurniture.selectedColorID = this.selectedColorID;
			newFurniture.selectedSizeID = this.selectedSizeID;
			newFurniture.mapPicSrc = this.mapPicSrc;
			newFurniture.dragPicSrc = this.dragPicSrc;
			newFurniture.modelSrc = this.modelSrc;
			newFurniture.modelZPosition = this.modelZPosition;
			return newFurniture;
		}
	}
}